<!DOCTYPE html> 
<html>
  <head>
    <!--META-->
	<meta charset="utf-8">
        
    <title>Scan journalier</title>
    <link href="css/mycss.css" type="text/css" rel="stylesheet">
    <script language="javascript" type="text/javascript">
///////////////////
//BLOCK DECLARATION
///////////////////
var idFirstLineScanTocs = 469;
var g_tabResponse = new Array();    
var g_refreshIntervalId;
var g_intVolumeTicket = 0;
var g_intNbElement = 0;
var g_exec_scan_inventaire = true;
var g_exec_scan_details = true;

//Block config DISE
var strBaliseScan = "javascript:PopUp('/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=";
var strBaliseScan_arch = "er=0 src=/AutoDiseWeb/images/archive_logo.gif alt='sig. archivée'><a href=\"javas";
var strUrlInventaireAutomate = "http://10.197.68.30:4000/AutoDiseWeb/automate/index.jsp?viewTarget=objectListeView&page=";
var strLabBlockAutomate = "http://10.197.68.30:4000/AutoDiseWeb/automate/popupModule.jsp?modType=SIG_SIG&modCode=";
//var strDomaine = "127.0.0.1/ODA_DISE";
var strDomaine = "10.85.32.5/ODA_DISE";

var lib_idle = "En attente ...";
var lib_inpro = "En cours ...";
var lib_ok = "Succes.";
var lib_ko = "Echec.";
var lib_cancel = "Annul&eacute;.";

///////////////////
//BLOCK FUNC METIER
///////////////////
function init() {
    try {
        initLastScan();
    }
    catch (er) {
        log(0, "ERROR(init):" + er.message);
    }
}

/**
 * Procedure principale du traitement
 * 
 */
function scanAutomateThread() {
    try {
        etape_1.innerHTML = lib_idle;
        etape_2.innerHTML = lib_idle;
        etape_3.innerHTML = lib_idle;
        etape_4.innerHTML = lib_idle;
        etape_5.innerHTML = lib_idle;
        etape_6.innerHTML = lib_idle;
        
        etape_1.innerHTML = lib_inpro;
        var t = setTimeout(function () {trait_etape1();},1000);
    }
    catch (er) {
        log(0, "ERROR(scanAutomateThread):" + er.message);
    } finally {
    }
}

/**
 * L'inventaire : Monter SQL.
 * 
 */
function trait_etape1() {
    try {
        setParameterL("maintenance", 1);
		
        log(0, "INFO(scanAutomate): D\351but du scan");

        setParameterL("init_last_scan_inventaire", getStrDateTime());
		
        InsertSQL("INSERT INTO `tab_histo_scan` (`id`, `type`, `date`, `volume`) VALUES (NULL, '1', NOW(), '0');");
        
        if(g_exec_scan_inventaire){
            viderInvTocs();
        }
            
        etape_1.innerHTML = lib_ok;
        etape_2.innerHTML = lib_inpro;
        var t = setTimeout(function () {trait_etape2();},1000);
    }
    catch (er) {
        log(0, "ERROR(trait_etape1):" + er.message);
    } finally {
    }
}

/**
 * L'inventaire : Scan de l'inventaire.
 * 
 */
function trait_etape2() {
    try {
        if(g_exec_scan_inventaire){
            g_intNbElement = 0;
        
            var lastPageAutomate = getParameterL("last_page_automate");

            var curPage = lastPageAutomate;
            var url = strUrlInventaireAutomate + curPage + "&modType=SIG_SIG&milis="+getMilise();
            while (isThere(url)) {
                curPage += 1;
                url = strUrlInventaireAutomate + curPage + "&modType=SIG_SIG&milis="+getMilise();
            }

            setParameterL("last_page_automate", curPage);

            for (var j = 1; j <= curPage; j++) {
                urlToScan = strUrlInventaireAutomate + j + "&modType=SIG_SIG&milis="+getMilise();
                xhr_object = new AJ();
                xhr_object.open("GET", urlToScan, false);
                xhr_object.send(null);
                sourceHtml = xhr_object.responseText;
                tabSourceHtml = sourceHtml.split("\n");
                sizeTabSourceHtml = tabSourceHtml.length - 2;

                g_intNbElement = getNbElement(tabSourceHtml, g_intNbElement);

                var i = idFirstLineScanTocs;

                while (i < sizeTabSourceHtml) {
                    strLine = tabSourceHtml[i];
                    while (((strLine.indexOf(strBaliseScan) == -1) & (strLine.indexOf(strBaliseScan_arch) == -1)) & (i < sizeTabSourceHtml)) {
                        i++;
                        strLine = tabSourceHtml[i];
                    }
                    if(i == sizeTabSourceHtml){
                        break;
                    }

                    //idToc
                    strLine = tabSourceHtml[i];
                    strIdToc = recuperer(strLine);

                    //resp
                    i = i + 2;
                    strLine = tabSourceHtml[i];
                    strResp = recuperer(strLine);

                    //date creation
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strDateCreat = recuperer(strLine);

                    //statut
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strStatut = recuperer(strLine);

                    //dateStatut
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strDateStatut = recuperer(strLine);

                    //liaisons
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strliaisons = "";

                    while (strLine.indexOf("</table></td>") == -1) {
                        i += 1;
                        strLine = tabSourceHtml[i];
                        strRecup = recuperer(strLine);
                        reg = new RegExp("[a-zA-Z0-9]", "g");
                        tabStrRecup = strRecup.match(reg);
                        if ((tabStrRecup != null) & (strRecup != "&nbsp;")) {
                            strliaisons += "$" + strRecup;
                        }
                    }

                    //priorite
                    strG = tabSourceHtml[i + 1].substring(22, 23);
                    while (strG != "P") {
                        i++;
                        strG = tabSourceHtml[i + 1].substring(22, 23);
                    }
                    i++;
                    strLine = tabSourceHtml[i];
                    strPriorite = recuperer(strLine);

                    //Emis
                    strLine = tabSourceHtml[i - 1];
                    strEmis = recuperer(strLine);

                    //libelle
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strFinLigne = strLine.substring(strLine.length - 6, strLine.length - 1);
                    strTagFinLigne = "</td>"
                    if (strFinLigne == strTagFinLigne) {
                        strLibelle = recuperer(strLine);
                    } else {
                        strLibelle = strLine.substring(22, strLine.length - 1);
                        strG = tabSourceHtml[i + 1].substring(1, 22);
                        while ((strG != "<td class=\"lineTabB\">") & (strG != "<td class=\"lineTabA\">")) {
                            i++;
                            strLine = tabSourceHtml[i];
                            strLibelle = strLibelle + strLine.substring(1, strLine.length - 6);
                            strG = tabSourceHtml[i + 1].substring(1, 22);
                        }
                    }
                    var reg = /[^éèàêàa-zA-Z0-9_\/-]/;
                    strLibelle = strLibelle.replace(reg, " ");

                    //origine
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strOrigine = recuperer(strLine);

                    //Application
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strApplication = recuperer(strLine);

                    //PF détection
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strDetection = recuperer(strLine);

                    //Duree
                    i = i + 1;
                    strLine = tabSourceHtml[i];
                    strDuree = recuperer(strLine);
                    if ((strDuree == 'ERROR') || (strDuree == '')) {
                        strDuree = tabSourceHtml[i+1];
                    }

                    insertTocsBase(strIdToc, strResp, strDateCreat, strStatut, strDateStatut, strliaisons, strPriorite, strLibelle, strOrigine, strApplication, strDetection, strDuree, strEmis);
                }
            }
        }
            
        etape_2.innerHTML = lib_ok;
        etape_3.innerHTML = lib_inpro;
        var t = setTimeout(function () {trait_etape3();},1000);
    }
    catch (er) {
        log(0, "ERROR(trait_etape2):" + er.message);
    } finally {
    }
}

/**
 * Le détail des tickets : Monter SQL.
 * 
 */
function trait_etape3() {
    try {
        if(g_exec_scan_inventaire){
            mountVmInventaireTocs();
		
            mountVmInventaireTocsVsr();

            var nb_inventaire_tocs = parseInt(getNbTocInventaire()[0][0]);
            if (nb_inventaire_tocs == g_intNbElement) {
                setParameterL("retour_scan_inventaire", "ok;input:" + g_intNbElement + ";bd:" + nb_inventaire_tocs);
            } else {
                setParameterL("retour_scan_inventaire", "ko;input:" + g_intNbElement + ";bd:" + nb_inventaire_tocs);
            }
        }
        
        var strDateTime = getStrDateTime();
        setParameterL("last_scan_inventaire", strDateTime);
		
        log(0, "INFO(scanAutomate): Fin du scan");
		
        InsertSQL("INSERT INTO `tab_histo_scan` (`id`, `type`, `date`, `volume`) VALUES (NULL, '2', NOW(), '"+nb_inventaire_tocs+"');");

        initLastScan();
            
        etape_3.innerHTML = lib_ok;
        etape_4.innerHTML = lib_inpro;
        var t = setTimeout(function () {trait_etape4();},1000);
    }
    catch (er) {
        log(0, "ERROR(trait_etape3):" + er.message);
    } finally {
    }
}

/**
 * Le détail des tickets : Monter SQL.
 * 
 */
function trait_etape4() {
    try {
        setParameterL("init_last_scan_tickets", getStrDateTime());
		
        InsertSQL("INSERT INTO `tab_histo_scan` (`id`, `type`, `date`, `volume`) VALUES (NULL, '3', NOW(), '0');");

        if(g_exec_scan_details){
            var volumeDetailTicket = getVolumeTicketsToScan();

            g_intVolumeTicket =  parseInt(volumeDetailTicket[0][0]);

            if( g_intVolumeTicket < 1200) {
                g_tabResponse = getTicketsToScan();

                g_refreshIntervalId = setInterval(function () {trait_etape5();}, 2000); 
            }else{
                setParameterL("retour_scan_tickets", "ko;La volum\351trie est trop importante. ("+g_intVolumeTicket+")");
                etape_4.innerHTML = lib_ko + "La volum&eacute;trie est trop importante. ("+g_intVolumeTicket+")";
                etape_5.innerHTML = lib_cancel;
                etape_6.innerHTML = lib_cancel;
            }
            
            etape_4.innerHTML = lib_ok;
            etape_5.innerHTML = lib_inpro + "(0/"+g_intVolumeTicket+")"; 
        }else{
            etape_4.innerHTML = lib_ok;
            etape_5.innerHTML = lib_inpro + "(0/"+g_intVolumeTicket+")"; 
        
            etape_5.innerHTML = lib_ok + "(0/"+g_intVolumeTicket+")";
            etape_6.innerHTML = lib_inpro;
            var t = setTimeout(function () {trait_etape6();},1000);
        }
    }
    catch (er) {
        log(0, "ERROR(trait_etape4):" + er.message);
    } finally {
    }
}

/**
 * Le détail des tickets : Scan des détails.
 * 
 */
function trait_etape5() {
    try {
        if(g_exec_scan_details){
            var resteAScan = g_tabResponse.length;
            if(resteAScan > 0){
                var strToc = g_tabResponse[0][0];
                scanTicket(strToc);
                g_tabResponse.splice(0, 1);
                var resteAScan = g_tabResponse.length;
                etape_5.innerHTML = lib_inpro + "("+(g_intVolumeTicket - resteAScan)+"/"+g_intVolumeTicket+")";
            }else{
                etape_5.innerHTML = lib_ok + "("+(g_intVolumeTicket - resteAScan)+"/"+g_intVolumeTicket+")";
                etape_6.innerHTML = lib_inpro;
                var t = setTimeout(function () {trait_etape6();},1000);
                clearInterval(g_refreshIntervalId);
            }
        }
    }
    catch (er) {
        log(0, "ERROR(trait_etape5):" + er.message);
    } finally {
    }
}

/**
 * Le détail des tickets : Descente SQL.
 * 
 */
function trait_etape6() {
    try {
        if(g_exec_scan_details){
            mountVmDurees();
            mountVmDureesEtapes();

            var nbTocInventaireRev0 = parseInt(getNbTocInventaireRev0());
            var nbTicketDetail = parseInt(getNbTicketDetail());

            if (nbTocInventaireRev0 == nbTicketDetail) {
                setParameterL("retour_scan_tickets", "ok;input:" + nbTocInventaireRev0 + ";bd:" + nbTicketDetail);
            } else {
                setParameterL("retour_scan_tickets", "ko;input:" + nbTocInventaireRev0 + ";bd:" + nbTicketDetail);
            }
        }

        var strDateTime = getStrDateTime();
        setParameterL("last_scan_tickets", strDateTime);
		
        InsertSQL("INSERT INTO `tab_histo_scan` (`id`, `type`, `date`, `volume`) VALUES (NULL, '4', NOW(), '"+g_intVolumeTicket+"');");

        setParameterL("maintenance", 0);

        etape_6.innerHTML = lib_ok;
        initLastScan();
    }
    catch (er) {
        log(0, "ERROR(trait_etape6):" + er.message);
    } finally {
    }
}

function scanTicket(id_toc) {
    try {
        var strNumTicket = id_toc;
        var htmlInfoTicket = getTicketInfo(strNumTicket);
        var tabHtmlInfo = new Array();
        tabHtmlInfo = htmlInfoTicket.split("\n");
        if (tabHtmlInfo.length > 600) {
            var tabHisto = new Array();
            tabHisto = getTab(tabHtmlInfo, "Historique");

            var tabResponsable = new Array();
            tabResponsable = getTab(tabHtmlInfo, "Responsable");
            
            var strDataCreat = "";
            strDataCreat = getElement(tabHtmlInfo, "Date de création");
            
            var strDernAction = "";
            strDernAction = getElement(tabHtmlInfo, "Dernière Action");

            var strStatut= "";
            strStatut = getElement(tabHtmlInfo, "Statut");

            var strDateStatut = "";
            strDateStatut = getElement(tabHtmlInfo, "Date de statut");

            var strVersion = "";
            strVersion = getElement(tabHtmlInfo, "Version");

            var strTransfert = "";
            strTransfert = getElement(tabHtmlInfo, "Transfert");
            
            var strDateReta = "";
            strDateReta = getElement(tabHtmlInfo, "Date de rétablissement");
            
            var strDateCloture = "";
            strDateCloture = getElement(tabHtmlInfo, "Date de cléture du ticket");
            
            var strEdsOri = "";
            strEdsOri = getElement(tabHtmlInfo, "Eds origine");
            
            var strEdsDest = "";
            strEdsDest = getElement(tabHtmlInfo, "Eds destinataire");
            
            var strTypeInit = "";
            strTypeInit = getElement(tabHtmlInfo, "Type initial");
            
            var strTypeSynch = "";
            strTypeSynch = getElement(tabHtmlInfo, "Type synchronisé");

            var strCodeFTcarto = "";
            strCodeFTcarto = getElement(tabHtmlInfo, "Code FT Carto");

            var strClassTick = "";
            strClassTick = getElement(tabHtmlInfo, "Classe ticket");

            var strPrio = "";
            strPrio = getElement(tabHtmlInfo, "Priorité");

            var strEmis = "";
            strEmis = getElement(tabHtmlInfo, "Emis");

            var strLib = "";
            strLib = getElement(tabHtmlInfo, "Libellé");

            var strAppl = "";
            strAppl = getElement(tabHtmlInfo, "Application");

            var strOri = "";
            strOri = getElement(tabHtmlInfo, "Origine");

            var strPFdec = "";
            strPFdec = getElement(tabHtmlInfo, "PF détection");

            var strType = "";
            strType = getElement(tabHtmlInfo, "Type");

            var strRcoor = "";
            strRcoor = getElement(tabHtmlInfo, "Suivi RCOOR");

            var strLibDet = "";
            strLibDet = getElement(tabHtmlInfo, "Libellé Détaillé");

            var strImpact = "";
            strImpact = getElement(tabHtmlInfo, "Impact");

            var strDesc = "";
            strDesc = getElement(tabHtmlInfo, "Description des causes");

            var strActions = "";
            strActions = getElement(tabHtmlInfo, "Actions");

            var strAlert = "";
            strAlert = getElement(tabHtmlInfo, "Alerte");

            var strPrioConso = "";
            strPrioConso = getElement(tabHtmlInfo, "Prioritée consolidée");

            var strDeman = "";
            strDeman = getElement(tabHtmlInfo, "Demandeur");

            var strCause = "";
            strCause = getElement(tabHtmlInfo, "Cause");

            var strCharg = "";
            strCharg = getElement(tabHtmlInfo, "Charges Consommées");

            var strAmelio = "";
            strAmelio = getElement(tabHtmlInfo, "Amélioration QS");

            var strNoFich = "";
            strNoFich = getElement(tabHtmlInfo, "No Fiche QS");

            var strCharConsoDis = "";
            strCharConsoDis = getElement(tabHtmlInfo, "Charges Consommées (DISE)");

            var strDom = "";
            strDom = getElement(tabHtmlInfo, "Domaine Fonctionnel");

            var tabParam = new Array();
            tabParam["id_toc"] = strNumTicket;
            if (tabResponsable[0][0] == "C est moi") {
                tabParam["responsable"] = "CMOI";
                depuis = tabResponsable[0][1];
                tabParam["resp_depuis"] = depuis.substring(10,depuis.lenght);
                tabParam["resp_nom"] = "";
                tabParam["resp_prenom"] = "";
            } else {
                tabParam["responsable"] = tabResponsable[0][1];
                tabParam["resp_depuis"] = tabResponsable[0][3];
                tabParam["resp_nom"] = tabResponsable[0][5];
                tabParam["resp_prenom"] = tabResponsable[0][7];
            }
            tabParam["date_creation"] = strDataCreat;
            tabParam["dernière_action"] = strDernAction;
            tabParam["statut"] = strStatut;
            tabParam["date_statut"] = strDateStatut;
            tabParam["version"] = strVersion;
            tabParam["transfert"] = strTransfert;
            tabParam["date_retablissement"] = strDateReta;
            tabParam["date_cloture"] = strDateCloture;
            tabParam["eds_origine"] = strEdsOri;
            tabParam["eds_destinataire"] = strEdsDest;
            tabParam["type_initial"] = strTypeInit;
            tabParam["type_synchronisé"] = strTypeSynch;
            tabParam["code_ft_carto"] = strCodeFTcarto;
            tabParam["classe_ticket"] = strClassTick;
            tabParam["priorite"] = strPrio;
            tabParam["emis"] = strEmis;
            tabParam["libelle"] = strLib;
            tabParam["application"] = strAppl;
            tabParam["origine"] = strOri;
            tabParam["pf_détection"] = strPFdec;
            tabParam["type"] = strType;
            tabParam["suivi_rcoor"] = strRcoor;
            tabParam["libelle_detaille"] = strLibDet;
            tabParam["impact"] = strImpact;
            tabParam["description_causes"] = strDesc;
            tabParam["actions"] = strActions;
            tabParam["alerte"] = strAlert;
            tabParam["prioritee_consolidee"] = strPrioConso;
            tabParam["demandeur"] = strDeman;
            tabParam["cause"] = strCause;
            tabParam["charges_consommees"] = strCharg;
            tabParam["amelioration_qs"] = strAmelio;
            tabParam["no_fiche_qs"] = strNoFich;
            tabParam["charges_consommees_dise"] = strCharConsoDis;
            tabParam["domaine_fonctionnel"] = strDom;

            insertTicket(tabParam);
            insertTicketHisto(strNumTicket, tabHisto);
            
        } else {
            log(0, "ERROR(scanTicket):" + strNumTicket);
        }
    
    }
    catch (er) {
        log(0, "ERROR(scanTicket):" + er.message);
    }
}

///////////////////
//BLOCK FUNC UTILS
///////////////////
function getTicketInfo(idToc) {
    var strResponse = "";
    try {
        var xhr_object = new AJ();
        xhr_object.open("GET", strLabBlockAutomate+idToc+"&milis="+getMilise(), false);
        xhr_object.send(null);

        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
        
        return strResponse;
    }
    catch (er) {
        log(0, "ERROR(getTicketInfo):" + er.message);
        return "ERROR";
    } finally {
        delete xhr_object;
    }
}

function isThere(url) {
    try {
        var xhr_object = new AJ();
        xhr_object.open("GET", url, false);
        xhr_object.send(null);

        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        responseSize = strResponse.length;

        if (responseSize > 17500) {
            return true;
        } else {
            return false;
        }

        return boolStatus;
    }
    catch (er) {
        log(0, "ERROR(isThere):" + er.message);
        return false;
    } finally {
        delete xhr_object;
    }
}

function AJ() {
    try {
        var obj;
        if (window.XMLHttpRequest) obj = new XMLHttpRequest();
        else if (window.ActiveXObject) {
            try {
                obj = new ActiveXObject('MSXML2.XMLHTTP.3.0');
            }
            catch (er) {
                try {
                    obj = new ActiveXObject("Microsoft.XMLHTTP");
                }
                catch (er) {
                    obj = false;
                }
            }
        }
        return obj;
    }
    catch (er) {
        log(0, "ERROR(AJ):" + er.message);
        return null;
    } 
}

function initLastScan() {
    try {
        var labLastScanTxt = "";

        labLastScanTxt = "<i>";

        var labDate = getParameterL("last_scan_inventaire");
        var labRetour = getParameterL("retour_scan_inventaire");

        labLastScanTxt += "Dernier scan : " + labDate + "</br>";
        labLastScanTxt += "Retour : " + labRetour + "</br>";
        labLastScanTxt += "</i>";

        var labLastScanTicketTxt = "";

        labLastScanTicketTxt = "<i>";

        var labDate = getParameterL("last_scan_tickets");
        var labRetour = getParameterL("retour_scan_tickets");

        labLastScanTicketTxt += "Dernier scan : " + labDate + "</br>";
        labLastScanTicketTxt += "Retour : " + labRetour + "</br>";
        labLastScanTicketTxt += "</i>";
        
        resume.innerHTML = "Etat de l'inventaire : <br>" + labLastScanTxt + "Etat des d&eacute;tails : <br>" + labLastScanTicketTxt;
    }
    catch (er) {
        log(0, "ERROR(initLastScan):" + er.message);
    }
}

function getElement(tabHtmlInfo, tag) {
    try {
        var strReturn = "";
        var tabHtml = new Array();
        tabHtml = tabHtmlInfo;
        var occu = 1;
        var find = 0;

        if (tag == "Version") {
            occu = 2;
        }

        for (var indice in tabHtml) {
            strLine = tabHtml[indice];
            strRecup = recuperer(strLine);
            i = parseInt(indice);

            if (strRecup == tag) {
                find += 1;
                if (find == occu) {
                    //recherche entete
                    i += 1;
                    strLine = tabHtml[i];

                    reg = new RegExp("[\t]", "g");
                    strLine = strLine.replace(reg, '');
                    reg = new RegExp("[\n]", "g");
                    strLine = strLine.replace(reg, "</br>");
                    reg = new RegExp("[\&*]", "g");
                    strLine = strLine.replace(reg, "ET");
                    
                    ligneComplete = (strLine.indexOf("</td>") != -1);
                    if (ligneComplete == true) {
                        strRecup = recuperer(strLine);
                        strReturn = strRecup;
                    } else {
                        reg = new RegExp("(<td)[^>]*.", "g");
                        strReturn = strLine.replace(reg, " ");
                        i += 1;
                        strLine = tabHtml[i];

                        reg = new RegExp("[\t]", "g");
                        strLine = strLine.replace(reg, '');
                        reg = new RegExp("[\n]", "g");
                        strLine = strLine.replace(reg, "</br>");
                        reg = new RegExp("[\&*]", "g");
                        strLine = strLine.replace(reg, "ET"); ;
                        
                        while (strLine.indexOf("</td>") == -1) {
                            strReturn += strLine;

                            i++;
                            strLine = tabHtml[i];

                            reg = new RegExp("[\t]", "g");
                            strLine = strLine.replace(reg, '');
                            reg = new RegExp("[\n]", "g");
                            strLine = strLine.replace(reg, "</br>");
                            reg = new RegExp("[\&*]", "g");
                            strLine = strLine.replace(reg, "ET");
                        }
                    }
                    break;
                }
            }
        }
        return strReturn;
    }
    catch (er) {
        log(0, "ERROR(getElement):" + er.message);
        return null;
    }
}

function getTab(tabHtmlInfo, tag, tagAlt) {
    try {
        var tabHtml = new Array();
        tabHtml = tabHtmlInfo;
        var tabRecup = new Array();
        var tabSubRecup = new Array();

        for (var indice in tabHtml) {
            strLine = tabHtml[indice];
            strRecup = recuperer(strLine);

            if ((strRecup == tag)|(strRecup == tagAlt)) {
                //recherche entete
                i = parseInt(indice) + 1;
                strLine = tabHtml[i];
                strRecup = recuperer(strLine);
                
                //on sort du titre
                while (strLine.indexOf("<tbody>") == -1) {
                    i += 1;
                    strLine = tabHtml[i];
                    strRecup = recuperer(strLine);
                }
                
                // on va récuper les entetes
                while (strRecup.length < 2) {
                    i += 1;
                    strLine = tabHtml[i];
                    strRecup = recuperer(strLine);
                }
                reg = new RegExp("[a-zA-Z0-9]", "g")
                while ((strRecup.length > 0) | (strLine.indexOf("</thead>") == -1)) {
                    tabStrRecup = strRecup.match(reg);
                    if (tabStrRecup != null) {
                        tabSubRecup[tabSubRecup.length] = strRecup;
                    }
                    i += 1;
                    strLine = tabHtml[i];
                    strRecup = recuperer(strLine);
                }

                nbCol = tabSubRecup.length;
                tabRecup[tabRecup.length] = tabSubRecup;
                tabSubRecup = new Array();

                //recherche data
                while ((strRecup.length < 2) & (strLine.indexOf("</tbody>") == -1)) {
                    i += 1;
                    strLine = tabHtml[i];
                    strRecup = recuperer(strLine);
                    reg = new RegExp("</a>[ ]*<a", "g")
                    if(strLine.match(reg) != null){
                        strRecup = "";
                    }
                }

                while (strRecup.length > 0) {
                    for (var j = 1; j <= nbCol; j++) {
                        tabSubRecup[tabSubRecup.length] = strRecup;
                        i += 1;
                        strLine = tabHtml[i];
                        strRecup = recuperer(strLine);
                        while ((strRecup.length < 2) & (strLine.indexOf("</tbody>") == -1)) {
                            i += 1;
                            strLine = tabHtml[i];
                            strRecup = recuperer(strLine);
                        }
                        if (strLine.indexOf("</tbody>") != -1) {
                            break;
                        }
                    }
                    if ((tabSubRecup.length == nbCol) | ((tag == "Objets liés ") | (tagAlt == "Objets liés /"))) {
                        tabRecup[tabRecup.length] = tabSubRecup;
                    }
                    tabSubRecup = new Array();
                    while ((strRecup.length < 2) & (strLine.indexOf("</tbody>") == -1)) {
                        i += 1;
                        strLine = tabHtml[i];
                        strRecup = recuperer(strLine);
                    }
                    if (strLine.indexOf("</tbody>") != -1) {
                        break;
                    }
                }
                break;
            }
        }
        return tabRecup;
    }
    catch (er) {
        log(0, "ERROR(getTab):" + er.message);
        return null;
    }
}

function recuperer(p_str) {
    var str = new String(p_str);
    if (str.length > 1) {
        var resultat = "none";
        try {
            //remplacer tous les tab
            reg = new RegExp("[\t]", "g")
            str = str.replace(reg, '');

            var reg = /(>)*[^<^>]+(<|$)/;
            resultat = str.match(reg);
            resultat = resultat[0];
            resultat = resultat.substring(1, resultat.length - 1);
            reg = /[^éèàêàa-zA-Z0-9_\/-]/;
            resultat = resultat.replace(reg, " ");

            if (resultat == "mg") {
                resultat = "";
            }
        }
        catch (er) {
            resultat = "";
        }

        if ((str.indexOf("></td>") != -1)&(resultat == "")) {
            resultat = "&nbsp;";
        }
        
        return resultat;
    } else {
        return str;
    }
}


function getStrDateTime() {
    try {
        var currentTime = new Date();
        var hours = pad2(currentTime.getHours());
        var minutes = pad2(currentTime.getMinutes());
        var annee = currentTime.getFullYear();
        var mois = pad2(currentTime.getMonth()+1);
        var jour = pad2(currentTime.getDate());
        var secondes = pad2(currentTime.getSeconds());
        var strDateTime = annee + "/" + mois + "/" + jour + " " + hours + ":" + minutes + ":" + secondes;
        return strDateTime;
    }
    catch (er) {
        log(0, "ERROR(getStrDateTime):" + er.message);
        return null;
    }
}

function pad2(number) {
    try {
        return (number < 10 ? '0' : '') + number;
    }
    catch (er) {
        log(0, "ERROR(pad2):" + er.message);
        return null;
    }
}

function getMilise() {
    var d = new Date();
    return d.getTime();
}

function getNbElement(tabSourceHtml, intNbElement) {
    try {
        if (intNbElement == 0) {
            i = 430;
            strLine = tabSourceHtml[i];
            while ((strLine.indexOf("submit") == -1) & (i < 450)) {
                i += 1;
                strLine = tabSourceHtml[i];
            }
            if (i == 450) {
                return 0;
            } else {
                var reg = /nbsp;/;
                resultat = strLine.split(reg);
                resultat = parseInt(resultat[2]);
                return resultat;
            }
        } else {
            return intNbElement;
        }
    }
    catch (er) {
        log(0, "ERROR(getNbElement):" + er.message);
        return null;
    }
}


///////////////////
//BLOCK FONCTIONS BD
///////////////////

function log(p_type, p_msg) {
    try {
        var strResponse = "";

        xhr_object = new AJ();
		
		var msg = p_msg.replace(/'/g, "\\'");
		var url = "http://"+strDomaine+"/phpsql/mysql_insertLog.php?milis="+getMilise()+"&type=" + p_type + "&msg=" + msg;

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
		print(er.message);
    } finally {
        delete xhr_object;
    }
}

function InsertSQL(sql) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/API/phpsql/mysql_InsertSQL.php?milis="+getMilise()+"&sql=" + sql, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(viderInvTocs):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function viderInvTocs() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_truncateInvTocs.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(viderInvTocs):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function mountVmInventaireTocs() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_mountVmInventaireTocs_lv1.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        } 

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_mountVmInventaireTocs.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(mountVmInventaireTocs):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function mountVmInventaireTocsVsr() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_mountTocsVsr.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        } 
    }
    catch (er) {
        log(0, "ERROR(mountVmInventaireTocsVsr):" + er.message);
    } finally {
        delete xhr_object;
    }
}


function mountVmDurees() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_mountDurees.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(mountVmDurees):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function mountVmDureesEtapes() {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_mountDureesEtapes.php?milis="+getMilise()+"&crtl=ok", false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(mountVmDureesEtapes):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function insertTocsBase(strIdToc, strResp, strDateCreat, strStatut, strDateStatut, strliaisons, strPriorite, strLibelle, strOrigine, strApplication, strDetection, strDuree, strEmis) {
    try {
        var tabParam = new Array();
        var strParam = "";
        var xhr_object = new AJ();
        var strReturn = "";

        tabParam["id_toc"] = strIdToc;
        tabParam["responsable"] = strResp;
        tabParam["date_creation"] = strDateCreat;
        tabParam["statut"] = strStatut;
        tabParam["date_statut"] = strDateStatut;
        tabParam["liaisons"] = strliaisons;
        tabParam["priorite"] = strPriorite;
        tabParam["libelle"] = strLibelle;
        tabParam["origine"] = strOrigine;
        tabParam["application"] = strApplication;
        tabParam["detection"] = strDetection;
        tabParam["duree"] = strDuree;
        tabParam["emis"] = strEmis;

        for (var indice in tabParam) {
            strParam += indice + "=" + tabParam[indice] + "&";
        }

        strParam = strParam.substring(0, strParam.length - 1);

        xhr_object.open("POST", "http://"+strDomaine+"/phpsql/mysql_insertTocs.php", false);
        xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr_object.send(strParam);
        if (xhr_object.readyState == 4) {
            strReturn = xhr_object.responseText;
        } else {
            strReturn = "ERROR";
        }

        if (strReturn.length > 4) {
            log(0, "ERROR(insertTocsBase) : "+ strReturn);
        }
    }
    catch (er) {
        log(0, "ERROR(insertTocsBase):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function insertTicketHisto(strIdToc, tab) {
    try {
        var tabParam = new Array();
        var strParam = "";
        var xhr_object = new AJ();
        var strReturn = "";

        var tabDocSize = tab.length;

        for (var i = 1; i < tabDocSize; i++) {
            tabParam["id_toc"] = strIdToc;
            tabParam["etat"] = tab[i][0];
            tabParam["resp"] = tab[i][1];
            tabParam["date"] = tab[i][2];

            for (var indice in tabParam) {
                strParam += indice + "=" + tabParam[indice] + "&";
            }

            strParam = strParam.substring(0, strParam.length - 1);

            xhr_object.open("POST", "http://"+strDomaine+"/phpsql/mysql_insertTicketStatut.php", false);
            xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xhr_object.send(strParam);
            if (xhr_object.readyState == 4) {
                strReturn = xhr_object.responseText;
            } else {
                strReturn = "ERROR";
            }

            if (strReturn.length > 4) {
                log(0, "ERROR(insertTicketHisto)");
            }
        }
    }
    catch (er) {
        log(0, "ERROR(insertTicketHisto):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function insertTicket(tab) {
    try {
        var strParam = "";
        var xhr_object = new AJ();
        var strReturn = "";

        for (var indice in tab) {
            strParam += indice + "=" + tab[indice] + "&";
        }

        strParam = strParam.substring(0, strParam.length - 1);

        xhr_object.open("POST", "http://"+strDomaine+"/phpsql/mysql_insertTicket.php", false);
        xhr_object.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr_object.send(strParam);
        if (xhr_object.readyState == 4) {
            strReturn = xhr_object.responseText;
        } else {
            strReturn = "ERROR";
        }

        if (strReturn.length > 4) {
            log(0, "ERROR(insertTicket)");
        }
    }
    catch (er) {
        log(0, "ERROR(insertTicket):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getParameterL(p_param_name) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/phpsql/mysql_getParamL.php?milis="+getMilise()+"&param_name=" + p_param_name, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        if (strResponse != "ERROR") {
            tabResponse = strResponse.split("$");
        }

        type = tabResponse[0];

        switch (type) {
            case "int":
                return parseInt(tabResponse[1]);
                break;
            case "varchar":
                return tabResponse[1];
                break;
            default:
                return tabResponse[1];
                break;
        }

    }
    catch (er) {
        log(0, "ERROR(getParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function setParameterL(p_param_name, p_param_value) {
    try {
        var strResponse = "";

        xhr_object = new AJ();

        xhr_object.open("GET", "http://"+strDomaine+"/API/phpsql/mysql_setParam.php?milis="+getMilise()+"&param_name=" + p_param_name + "&param_value=" + p_param_value, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getTicketsToScan() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "http://"+strDomaine+"/phpsql/mysql_getTicketsToScan.php?milis=" + getMilise() + "&crtl=ok";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getVolumeTicketsToScan() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "http://"+strDomaine+"/phpsql/mysql_getVolumeTicketsToScan.php?milis=" + getMilise() + "&crtl=ok";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getNbTocInventaire() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "http://"+strDomaine+"/phpsql/mysql_getNbTocInventaire.php?milis=" + getMilise() + "&ctrl=ok";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getNbTocInventaireRev0() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "http://"+strDomaine+"/phpsql/mysql_getNbTocInventaireRev0.php?milis=" + getMilise() + "&ctrl=ok";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}

function getNbTicketDetail() {
    try {
        var strResponse = "";
        var returns = new Array();
        var tempTab = new Array();
        var tempSubTab = new Array();

        xhr_object = new AJ();

        var url = "http://"+strDomaine+"/phpsql/mysql_getNbTicketDetail.php?milis=" + getMilise() + "&ctrl=ok";

        xhr_object.open("GET", url, false);
        xhr_object.send(null);
        if (xhr_object.readyState == 4) {
            strResponse = xhr_object.responseText;
        } else {
            strResponse = "ERROR";
        }

        tempTab = strResponse.split("\n");

        for (var indice in tempTab) {
            tempSubTab = tempTab[indice].split("&");
            if (tempSubTab.length > 1) {
                tab = new Array(tempSubTab[0], parseFloat(tempSubTab[1]));
                returns[returns.length] = tab;
            }
        }

        return returns;
    }
    catch (er) {
        log(0, "ERROR(setParameter):" + er.message);
    } finally {
        delete xhr_object;
    }
}


</script>
  </head>
  <body onload="init();">
    <table style="text-align: left; width: 100%; height: 100%;" border="0" cellpadding="0" cellspacing="0">
      <tbody>
        <tr>
          <td style="height: 100px; background-color: rgb(204, 204, 204);">
            <table style="text-align: left; width: 100%; height: 100%;" border="0" cellpadding="0" cellspacing="0">
              <tbody>
                <tr>
                  <td style="width: 10px;"></td>
                  <td style="text-align: center; width: 200px;">
                    <img src="img/logo_CGI.gif" />
                  </td>
                  <td></td>
                  <td style="width: 200px;"></td>
                  <td style="width: 10px;"></td>
                </tr>
              </tbody>
            </table>
          </td>
        </tr>
        <tr>
          <td style="background-color:#E31937; height: 2px;"></td>
        </tr>
        <tr align="center" valign="top">
          <td>
              <table cellpadding="0" cellspacing="0" style="text-align: left; width: 100%; height: 100%;">
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                          &nbsp;</td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">&nbsp;</td>
                      <td style="text-align: center">&nbsp;</td>
                      <td style="width: 10px;">&nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;"></td>
                      <td style="height: 2px; background-color: rgb(204, 204, 204);"></td>
                      <td style="width: 10px;"></td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                      <center>
                      <FORM method="post" id="formCreerDossier">
                           
                            <table style="text-align:left;">
                                <tr>
                                    <td colspan="2" style="text-align:center;">
                                         <label id="resume"></label>
                                    </td>
                                </tr>
                                 <tr>
                                    <td colspan="2" style="text-align:center;">
                                         <a href="javascript:scanAutomateThread();">Lancer le scan d'Automate</a>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        L'inventaire : Monter SQL.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_1"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        L'inventaire : Scan de l'inventaire.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_2"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        L'inventaire : Descente SQL.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_3"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Le détail des tickets : Monter SQL.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_4"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Le détail des tickets : Scan des détails.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_5"></label>
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        Le détail des tickets : Descente SQL.
                                    </td>
                                    <td style="with:100">
                                        <label id="etape_6"></label>
                                    </td>
                                </tr>
                            </table>
                        </FORM>
                      </center>   
                      </td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
                  <tr>
                      <td style="width: 10px;">
                          &nbsp;</td>
                      <td>
                          &nbsp;</td>
                      <td style="width: 10px;">
                          &nbsp;</td>
                  </tr>
              </table>
            </td>
        </tr>
        <tr>
          <td style="background-color: silver; height: 2px;"></td>
        </tr>
        <tr>
          <td style="height: 100px;"></td>
        </tr>
      </tbody>
    </table>
  </body>
</html>